package com.slz3.business.frame.module;

import com.slz3.business.frame.annotation.ConfigDomain;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.VE;

import java.util.List;
import java.util.UUID;

import static java.util.stream.Collectors.toList;

public abstract class AbstractDomainRedisKey<D> implements DomainRedisKey {

    private Class<D> clazz;

    public AbstractDomainRedisKey() {
        this.clazz = (Class<D>) Steward.getTClass(getClass());
    }

    @Override
    public String entity(UUID uuid) {
        if (uuid == null) {
            return null;
        }
        return String.format("%s:%s:DataJsonStr:%s",
                ConfigDomain.class.getSimpleName(),
                clazz.getSimpleName(),
                uuid.toString()
        );
    }

    @Override
    public List<String> entityList(List<UUID> uuidList) {
        if (VE.isEmpty(uuidList)) {
            return null;
        }
        return uuidList.stream().map(item -> entity(item)).collect(toList());
    }

    @Override
    public String uuidList() {
        return String.format("%s:%s:%s",
                ConfigDomain.class.getSimpleName(),
                clazz.getSimpleName(),
                "UuidList"
        );
    }

    @Override
    public String uuidSet() {
        return String.format("%s:%s:%s",
                ConfigDomain.class.getSimpleName(),
                clazz.getSimpleName(),
                "UuidSet"
        );
    }

    @Override
    public String uuidListByUpper(UUID upper) {
        return String.format("%s:%s:%s:%s",
                ConfigDomain.class.getSimpleName(),
                clazz.getSimpleName(),
                "UuidList",
                upper
        );
    }

    @Override
    public String uuidSetByUpper(UUID upper) {
        return String.format("%s:%s:%s:%s",
                ConfigDomain.class.getSimpleName(),
                clazz.getSimpleName(),
                "UuidSet:Upper",
                upper
        );
    }
}
